package com.codefish.framework.spring.beans.factory.config;

import com.codefish.framework.spring.core.utils.StringUtils;

import java.util.Properties;

/**
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/11/04 下午 02:07
 */
public abstract class PlaceholderConfigurerSupport extends PropertyResourceConfigurer {
    public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";
    public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";

    protected String placeholderPrefix = DEFAULT_PLACEHOLDER_PREFIX;
    protected String placeholderSuffix = DEFAULT_PLACEHOLDER_SUFFIX;

    /**
     * 解析字符串的值
     * @param properties 属性资源对象
     * @param strVal 目标字符串值
     * @return 字符串值解析后的值
     */
    protected  String resolveValue(Properties properties, String strVal){
        String value = strVal;
        if (!StringUtils.isEmpty(strVal) && strVal.startsWith(placeholderPrefix) && strVal.endsWith(placeholderSuffix)) {
            String propKey = strVal.substring(placeholderPrefix.length(), strVal.length() - placeholderSuffix.length());
            value = properties.getProperty(propKey);
            if (value == null) {
                value = strVal;
            }
        }
        return value;
    }

}
